﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit
{
    
    public partial class UCTerrainData : UserControl
    {
        PListRoot root;
        String cczDirector;

        public UCTerrainData()
        {
            InitializeComponent();
        }
        public UCTerrainData(String cczDirector_)
        {
            cczDirector = cczDirector_;
            root = PListRoot.Load(cczDirector_ + "\\TerrainData.plist");
            InitializeComponent();
            LoadTerrains();

        }
        public void SaveAllData()
        {
            PListDict rootDic = (PListDict)root.Root;
            PListDict battlesDic = (PListDict)rootDic["Move"];
            foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
            {
                if (t.Value == "所有兵种")
                {
                    break;
                }

            }
            root.Save(cczDirector + "\\TerrainData.plist");
        }
        void LoadTerrains()
        {
            
            dataGridView1.Columns.Add("ColumName", "兵种");
            dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Gray;
            dataGridView1.Columns[0].ReadOnly = true;

            foreach( KeyValuePair<int,String> t in Constant.terrainDic)
            {
                dataGridView1.Columns.Add("Colum"+t.Key, t.Value);
            }
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            dataGridView1.AllowUserToAddRows = false;
        }
        void ShowMagics()
        {
            PListDict rootDic = (PListDict)root.Root;
            PListDict magicsDic =(PListDict) rootDic["Magics"];
            foreach (KeyValuePair<int, String> t in Constant.magicTypeDic)
            {
                PListArray array = (PListArray)magicsDic[t.Key.ToString()];
                for (int i = 0; i != array.Count; ++i)
                {
                    PListInteger cell = (PListInteger)array[i];
                    dataGridView1.Rows[t.Key].Cells[i+1].Value = cell.Value;

                }

            }
        }
        void ShowBattles()
        {
            PListDict rootDic = (PListDict)root.Root;
            PListDict battlesDic = (PListDict)rootDic["Battle"];
            foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
            {
                if (t.Value == "所有兵种")
                {
                    break;
                }
                PListArray array = (PListArray)battlesDic[t.Key.ToString()];
                for (int i = 0; i != array.Count; ++i)
                {
                    PListInteger cell = (PListInteger)array[i];
                    dataGridView1.Rows[t.Key].Cells[i + 1].Value = cell.Value;

                }

            }

        }
        void ShowMove()
        {
            PListDict rootDic = (PListDict)root.Root;
            PListDict battlesDic = (PListDict)rootDic["Move"];
            foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
            {
                if (t.Value == "所有兵种")
                {
                    break;
                }
                PListArray array = (PListArray)battlesDic[t.Key.ToString()];
                for (int i = 0; i != array.Count; ++i)
                {
                    PListInteger cell = (PListInteger)array[i];
                    dataGridView1.Rows[t.Key].Cells[i + 1].Value = cell.Value;

                }

            }
        }
        private void radioButtonMagic_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();

            foreach (KeyValuePair<int, String> t in Constant.magicTypeDic)
            {
                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = t.Value;
                
            }
            
            ShowMagics();
        }

        private void radioButtonBattle_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
            {
                int index = this.dataGridView1.Rows.Add();

                this.dataGridView1.Rows[index].Cells[0].Value = t.Value;
            }
            ShowBattles();
        }

        private void radioButtonMove_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();

            foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
            {
                int index = this.dataGridView1.Rows.Add();

                this.dataGridView1.Rows[index].Cells[0].Value = t.Value;
            }
            ShowMove();
        }



        private void buttonSave_Click(object sender, EventArgs e)
        {
            PListDict rootDic = (PListDict)root.Root;
            if (radioButtonMagic.Checked)
            {
                PListDict magicsDic =(PListDict) rootDic["Magics"];
                magicsDic.Clear();
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {                 
                    foreach (KeyValuePair<int, String> t in Constant.magicTypeDic)
                    {
                        if ((String)row.Cells[0].Value == t.Value)
                        {
                            PListArray magicArray = new PListArray();
                            for (int i = 1; i != dataGridView1.Columns.Count; ++i)
                                magicArray.Add(new PListInteger(Convert.ToInt64(row.Cells[i].Value)));
                            magicsDic.Add(t.Key.ToString(), magicArray);
                            break;
                        }

                    }
                }

            }
            else
            {
                PListDict teamsTileDic;

                if (radioButtonBattle.Checked)
                    teamsTileDic = (PListDict)rootDic["Battle"];
                else
                    teamsTileDic = (PListDict)rootDic["Move"];
                teamsTileDic.Clear();
                foreach (KeyValuePair<int, String> t in Constant.teamTypeDic)
                {
                    PListArray teamArray = new PListArray();
                    for (int i = 1; i != dataGridView1.Columns.Count; ++i)
                    {
                       
                            teamArray.Add(new PListInteger(Convert.ToInt64(dataGridView1.Rows[t.Key].Cells[i].Value)));
                    }
                    teamsTileDic.Add(t.Key.ToString(), teamArray);
                }

            }


            
        }

    }

}
